import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  Query,
  UseGuards,
  ValidationPipe,
} from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';
import { DepartmentService } from './department.service';
import { AddRoleDto } from './dto/add-role.dto';
import { CreateDepartmentDto } from './dto/create-department.dto';
import { UpdateDepartmentDto } from './dto/update-department.dto';

@Controller('department')
export class DepartmentController {
  constructor(private readonly departmentService: DepartmentService) {}

  @Post('add')
  @UseGuards(AuthGuard('jwt')) //jwt策略，身份鉴权
  create(@Body(new ValidationPipe()) createDepartmentDto: CreateDepartmentDto) {
    return this.departmentService.create(createDepartmentDto);
  }

  @Get('list')
  @UseGuards(AuthGuard('jwt')) //jwt策略，身份鉴权
  findAll(
    @Query('departmentname') departmentname: string,
    @Query('pageSize') pageSize: number,
    @Query('currentPage') currentPage: number,
  ) {
    return this.departmentService.findAll(
      departmentname ? departmentname : '',
      {
        pageSize: pageSize ? pageSize : 100,
        currentPage: currentPage ? currentPage - 1 : 0,
      },
    );
  }

  @Get(':id')
  @UseGuards(AuthGuard('jwt')) //jwt策略，身份鉴权
  findOne(@Param('id') id: string) {
    return this.departmentService.findOne(+id);
  }

  @Patch(':id')
  @UseGuards(AuthGuard('jwt')) //jwt策略，身份鉴权
  update(
    @Param('id') id: string,
    @Body() updateDepartmentDto: UpdateDepartmentDto,
  ) {
    return this.departmentService.update(id, updateDepartmentDto);
  }

  @Delete(':id')
  @UseGuards(AuthGuard('jwt')) //jwt策略，身份鉴权
  remove(@Param('id') id: string) {
    return this.departmentService.remove(id);
  }

  @Post('/add/role')
  @UseGuards(AuthGuard('jwt')) //jwt策略，身份鉴权
  addRole(@Body() addRoleDto: AddRoleDto) {
    //开启事务第三步：获取事务管理器)
    return this.departmentService.addRole(addRoleDto);
  }

  @Get('/hasroles/:id')
  @UseGuards(AuthGuard('jwt')) //jwt策略，身份鉴权
  async findOwnerRoles(@Param('id') id: string) {
    return await this.departmentService.findOwnerRoles(id);
  }
}
